package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.Rate;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.RateService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "rate")
public class RateController {

    private static final Logger logger = LoggerFactory.getLogger(RateController.class);

    @Autowired
    private RateService rateService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private FreightService freightService;
    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private ErrandService errandService;
    @Autowired
    private ZhuanxianService zhuanxianService;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("rate")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("driverrate/add");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "add/{id}", method = RequestMethod.GET)
    public ModelAndView add(@PathVariable("id") Long driverId) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("rate")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("driverrate/add");
            mv.addObject("driver", driverService.findOne(driverId));

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(Rate rate, boolean goOn, boolean page2) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("rate")) {

            rate.setTitle("无");
            rate.setMemberId(0L);
            rate.setOrderId(0L);
            rate.setOrderNumber(null);
            rate.setManager(true);
            rateService.insertRecord(rate);

            String detail = "【%s】新增了服务人员评价【" + rate.getContent() + "】";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            if (goOn) {
                if (page2) {
                    return "redirect:add/" + rate.getDriverId();
                } else {
                    return "redirect:add";
                }
            } else {
                if (page2) {
                    return "redirect:listByDriver/" + rate.getDriverId();
                } else {
                    return "redirect:list";
                }
            }

        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(@RequestParam(required = false, value = "param") String param) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("rate")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("driverrate/list");

            mv.addObject("page", rateService.queryAll(param));

            mv.addObject("param", param);

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "listByDriver/{id}", method = RequestMethod.GET)
    public ModelAndView listByDriver(@PathVariable("id") Long driverid,
        @RequestParam(required = false, value = "start") Date start,
        @RequestParam(required = false, value = "end") Date end) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("rate")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("driverrate/listbydriver");

            mv.addObject("page", rateService.queryAll(driverid, start, end));

            mv.addObject("start", start);
            mv.addObject("end", end);
            mv.addObject("driver", driverService.findOne(driverid));

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> delete(Long id, String orderNo) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("rate")) {
            Result<Integer> result = new Result<Integer>();

            if (null != id) {
                Rate rate = null;
                if (StringUtils.isNotBlank(orderNo)) {
                    if (orderNo.contains("ZC")) {
                        rate = zhuancheService.findRateOne(id);
                        if (null != rate) {
                            zhuancheService.deleteRateOne(id);
                            String detail =
                                "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel()
                                    + "星";
                            OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                                detail);
                            applicationEventMulticaster.multicastEvent(operationLogEvent);

                            result.setSuccess(true);
                            result.setMessage("删除成功");
                        } else {
                            result.setSuccess(false);
                            result.setMessage("没有可以删除的数据");
                        }
                    } else if (orderNo.contains("PT")) {
                        rate = errandService.findRateOne(id);
                        if (null != rate) {
                            errandService.deleteRateOne(id);
                            String detail =
                                "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel()
                                    + "星";
                            OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                                detail);
                            applicationEventMulticaster.multicastEvent(operationLogEvent);

                            result.setSuccess(true);
                            result.setMessage("删除成功");
                        } else {
                            result.setSuccess(false);
                            result.setMessage("没有可以删除的数据");
                        }
                    } else if (orderNo.contains("HY")) {
                        rate = freightService.findRateOne(id);
                        if (null != rate) {
                            freightService.deleteRateOne(id);
                            String detail =
                                "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel()
                                    + "星";
                            OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                                detail);
                            applicationEventMulticaster.multicastEvent(operationLogEvent);

                            result.setSuccess(true);
                            result.setMessage("删除成功");
                        } else {
                            result.setSuccess(false);
                            result.setMessage("没有可以删除的数据");
                        }
                    } else if (orderNo.contains("CJZX")) {
                        rate = zhuanxianService.findRateOne(id);
                        if (null != rate) {
                            zhuanxianService.deleteRateOne(id);
                            String detail =
                                "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel()
                                    + "星";
                            OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                                detail);
                            applicationEventMulticaster.multicastEvent(operationLogEvent);

                            result.setSuccess(true);
                            result.setMessage("删除成功");
                        } else {
                            result.setSuccess(false);
                            result.setMessage("没有可以删除的数据");
                        }
                    } else {
                        rate = rateService.findOne(id);
                        if (null != rate) {
                            String detail =
                                "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel()
                                    + "星";

                            rateService.delete(rate);

                            OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                                detail);
                            applicationEventMulticaster.multicastEvent(operationLogEvent);

                            result.setSuccess(true);
                            result.setMessage("删除成功");
                        } else {
                            result.setSuccess(false);
                            result.setMessage("没有可以删除的数据");
                        }
                    }
                } else {
                    rate = rateService.findOne(id);
                    if (null != rate) {
                        String detail =
                            "【%s】删除了服务人员评价【" + rate.getContent() + "】，" + rate.getStarLevel() + "星";
                        rateService.delete(rate);
                        OperationLogEvent operationLogEvent = new OperationLogEvent(principal,
                            detail);
                        applicationEventMulticaster.multicastEvent(operationLogEvent);
                        result.setSuccess(true);
                        result.setMessage("删除成功");
                    } else {
                        result.setSuccess(false);
                        result.setMessage("没有可以删除的数据");
                    }
                }
            } else {
                result.setSuccess(false);
                result.setMessage("没有可以删除的数据");
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}
